package com.example.updatedemo;

import java.util.List;

import com.lidroid.xutils.view.annotation.ViewInject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
		* @description 弹出的快速更新电子书包所有程序的显示adapter
		* @version 2.0
		* @author panyuanyuan
		* @update 2012-9-17 下午5:34:12
		*/
public class UpdateQuickAdapter1 extends BaseAdapter {
	private Context context;
	private List<T_Version> versions;
	private LayoutInflater inflater;
	String upgradeIp;
	MainApplication app;
	String appname;
	private Button button;

	public UpdateQuickAdapter1(Context context, List<T_Version> versions,Button button) {
		this.context = context;
		this.versions = versions;
		inflater = (LayoutInflater) context
				.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		
		app = (MainApplication) context.getApplicationContext();
		this.button = button;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return versions.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return versions.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		final T_Version version = versions.get(position);
		//if (convertView == null) {
		convertView = inflater.inflate(R.layout.download_item, null);
		holder = new ViewHolder();
		holder.name = (TextView) convertView.findViewById(R.id.download_name);
		holder.state = (TextView) convertView
				.findViewById(R.id.download_state);
		holder.progressBar = (ProgressBar) convertView.findViewById(R.id.download_pb);
		holder.state_iv = (ImageView)convertView.findViewById(R.id.download_image);
		holder.pro_tv = (TextView)convertView.findViewById(R.id.download_tv);
		

		/*convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}*/
		if(UpdateActivity.btn_state.equals("install")||UpdateActivity.btn_state.equals("install_ok"))
		{
			holder.state.setText("正在安装");

        	
            boolean hasApp = Utils.hasApp(context, version.getProduct());
            //机器有此应用，再判断版本，是最新版本，安装完成
            if(hasApp)
            {
            	
            	PackageInfo oldPackageInfo;
				try {
					oldPackageInfo = context.getPackageManager()
							.getPackageInfo(version.getProduct(), 0);
					int isUpgrade = Utils.isVersionUpdate(
							version.getVersion(), oldPackageInfo);
					if(isUpgrade==2)
					{
						holder.state.setText("安装完成");
						holder.pro_tv.setVisibility(View.GONE);
						holder.state_iv.setVisibility(View.VISIBLE);
						holder.state_iv.setBackgroundResource(R.drawable.finish);	
						version.setState("success");
					}
	            	
				} catch (NameNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
            }
            
        	int j = 0;
        	String str;
        	for (int i1 = 0; i1 < versions.size(); i1++) {
        		str = versions.get(i1).getState();
        		if(str!=null&&!"".equals(str))
        		{
        			if(str.equals("success"))
    				{
    					j++;
    				}
        		}
					
			}
        	System.out.println("j-----------"+j);
        	if(j==versions.size())
        	{
        		UpdateActivity.btn_state = "install_ok";
//        		button.setText("安装完成");	
        		button.setBackgroundResource(R.drawable.btn_done_install);
        		
        	}
		}
		else if(UpdateActivity.btn_state.equals("download"))
		{
			holder.state.setText("未下载");
		}
					
		holder.name.setText(version.getFilename());
		holder.progressBar.setVisibility(View.INVISIBLE);
		
		
		return convertView;
	}

//	//点击下载监听
//	OnDownloadClickLinstener linstener;
//
//	public void setOnDownloadClickLinstener(OnDownloadClickLinstener linstener) {
//		this.linstener = linstener;
//	}
//
//	public interface OnDownloadClickLinstener {
//		void OnDownloadClick(T_Version version);
//	}

	static class ViewHolder {
	
		public TextView name;//文件名
	      
		public TextView state;//下载状态名
	      
		public ProgressBar progressBar;//下载进度bar
		
		public ImageView state_iv;//下载状态图标
		
		public TextView pro_tv;//进度显示
	}
}
